package com.gm.wholesale.module.settlement.view;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.github.gzuliyujiang.calendarpicker.CalendarPicker;
import com.github.gzuliyujiang.calendarpicker.OnRangeDatePickListener;
import com.gm.commonlib.util.timeUtils.DateTimeUtils;
import com.gm.commonlib.views.superview.SuperTextView;
import com.gm.commonlib.views.timePickerView.TimePickerViewUtils;
import com.gm.wholesale.R;

import java.util.Date;

public class AccuntHeaderView extends LinearLayout {
    private Long startTime;
    private Long endTime;
    private Context mContext;
    public TextView choiceTimeTv;
    private CalendarPicker mCalendarPicker;
    private CheckBox checkBox;
    private SuperTextView toAccountBtn;
    private OnFunctionListener mListener;

    public void reset(){
        checkBox.setChecked(false);
        startTime=System.currentTimeMillis();
        endTime=startTime;
        String times=DateTimeUtils.formatDateTime(startTime,DateTimeUtils.HMD2)+" 至 "+DateTimeUtils.formatDateTime(endTime,DateTimeUtils.HMD2);
        choiceTimeTv.setText(times);
    }


    public void setOnFunctionListener(OnFunctionListener listener){
        this.mListener=listener;
    }

    public interface OnFunctionListener{
        void onAllChecked(boolean mIsCheck);
        void onToAccount();
        void onChoiceTime(Date startTime,Date endTime);
    }

    public void setAllChecked(boolean isCheck){
      checkBox.setChecked(isCheck);
    }


    public AccuntHeaderView(Context context) {
        super(context);
        initView(context);
    }

    public AccuntHeaderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }
    private void initView(Context context){

        startTime=System.currentTimeMillis();
        endTime=startTime;
        mContext=context;
        LayoutInflater.from(context).inflate(R.layout.view_accunt_header,this);
        choiceTimeTv=findViewById(R.id.choiceTimeTv);
        String times=DateTimeUtils.formatDateTime(startTime,DateTimeUtils.HMD2)+" 至 "+DateTimeUtils.formatDateTime(endTime,DateTimeUtils.HMD2);
        choiceTimeTv.setText(times);
        checkBox = findViewById(R.id.checkBox);
        toAccountBtn = findViewById(R.id.toAccountBtn);
        choiceTimeTv.setOnClickListener(v -> {
            mCalendarPicker = TimePickerViewUtils.showTimePicker((Activity) mContext,startTime,endTime);
            if (mCalendarPicker!=null){
                mCalendarPicker.setOnRangeDatePickListener((startDate, endDate) -> {
                    startTime=startDate.getTime();
                    endTime=endDate.getTime();
                    String times1 =DateTimeUtils.formatDateTime(startDate,DateTimeUtils.HMD2)+" 至 "+DateTimeUtils.formatDateTime(endDate,DateTimeUtils.HMD2);
                    choiceTimeTv.setText(times1);
                    if (mListener!=null){
                        mListener.onChoiceTime(startDate,endDate);
                    }
                });
            }
        });
        findViewById(R.id.checkAllTv).setOnClickListener(v -> {
            checkBox.setChecked(!checkBox.isChecked());
            mListener.onAllChecked(checkBox.isChecked());
        });

        checkBox.setOnClickListener(v -> mListener.onAllChecked(checkBox.isChecked()));

        toAccountBtn.setOnClickListener(v -> {
            if (mListener!=null){
                mListener.onToAccount();
            }
        });

    }
}
